using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Diagnostics;

namespace _1945
{
    /// <summary>
    /// This is a game component that implements IUpdateable and IDrawable.
    /// </summary>
    public class HighScore : Microsoft.Xna.Framework.DrawableGameComponent
    {

        SpriteBatch spriteBatch;
        SpriteFont spriteFont;

        string title = "";
        public enum titleMessage { gameOver, victory };
        
        Dictionary<int, string> score;
        KeyboardState keyboardState;

        public HighScore(Game game)
            : base(game)
        {
            score = HighScoreManager.readScore();
            spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            spriteFont = (SpriteFont)Game.Services.GetService(typeof(SpriteFont));

        }

        public void setTitle(titleMessage t)
        {
            switch (t)
            {
                case titleMessage.gameOver:
                    title = "Game over";
                    break;
                case titleMessage.victory:
                    title = "Congratulations - level completed";
                    break;
                default:
                    break;
            }
            score = HighScoreManager.readScore();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            keyboardState = Keyboard.GetState();

            if (keyboardState.IsKeyDown(Keys.Escape)){
                ((Game1)Game).setGameScene(Game1.GameScenes.startScene);
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// Called when the DrawableGameComponent needs to be drawn. Override this method with component-specific drawing code.
        /// Reference page contains links to related conceptual articles.
        /// </summary>
        /// <param name="gameTime">Time passed since the last call to Microsoft.Xna.Framework.DrawableGameComponent.Draw(Microsoft.Xna.Framework.GameTime).</param>
        public override void Draw(GameTime gameTime)
        {
            // TODO: Add your draw code here
            String ss ="";
            String sd = "";
            spriteBatch.Begin();

                spriteBatch.DrawString(spriteFont, title, new Vector2(100, 50), Color.White);
                spriteBatch.DrawString(spriteFont, "High Score", new Vector2(100), Color.White);

                for (int i = 0; i < score.Count && i < 10; i++)
                {
                    ss = score.ElementAt(i).Key.ToString();
                    sd = score.ElementAt(i).Value.ToString();
                    spriteBatch.DrawString(spriteFont, ss, new Vector2(100, 100 + 50 * (i + 1)), Color.White);
                    spriteBatch.DrawString(spriteFont, sd, new Vector2(250, 100 + 50 * (i + 1)), Color.White);
                }

                spriteBatch.DrawString(spriteFont, "Press 'Esc' to get back to start menu", new Vector2(100, Game.Window.ClientBounds.Height - 50), Color.White);

            spriteBatch.End();

            base.Draw(gameTime);
        }


    }
}